package project.a1;

import java.util.ArrayList;

/**
 * Student name : Sayaka Ozaki
 * Student ID   : 12017111
 * This is GridView class for CP1300 A1.
 */
public class GridView {
    private char[][] grid;
    private ArrayList<Car> carArr;

    //private ProjModel proj;

    public GridView(ProjModel proj) {
        int hn = proj.getHorLaneNum();
        int vn = proj.getVerLaneNum();
        //grid = new char[hn][vn];
        int gs = proj.getGridSize();
        grid = new char[gs][gs];
        //this.proj = proj;
        setAll('#');
        buildHorStreet(hn);
        buildVerStreet(vn);
        carArr = new ArrayList<Car>();
        carArr.add(new Car());
    }

    private void buildVerStreet(int vn) {
        int nc = grid.length;
        int middle = nc / 2;
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid.length; c++) {
                if (c == middle) {
                    grid[r][c] = ' ';
                }
            }
        }
    }

    private void buildHorStreet(int horLaneNum) {
        int nr = grid.length;
        int middle = nr / 2;
        for (int r = 0; r < nr; r++) {
            char[] row = grid[r];
            if (r == middle) {
                setRow(row, ' ');
            }
        }
    }

    private void setRow(char[] row, char c) {
        for (int i = 0; i < row.length; i++) {
            row[i] = c;
        }
    }

    private void setAll(char ch) {
        for (int r = 0; r < grid.length; r++) {
            char[] row = grid[r];
            for (int c = 0; c < row.length; c++) {
                grid[r][c] = ch;
            }
        }

    }

    public void set(int r, int c, char v) {
        grid[r][c] = v;
    }

    public void show() {
        setCarsOnGrid();
        showGrid();
    }

    private void setCarsOnGrid() {
        for (int i = 0; i < carArr.size(); i++) {
            Car car =  carArr.get(i);  
        }
    }

    public void showGrid() {
        for (int r = 0; r < grid.length; r++) {
            char[] row = grid[r];
            for (int c = 0; c < row.length; c++) {
                char ch = row[c];
                System.out.print(ch);
            }
            System.out.println(" ");
        }
    }
}
